package com.twb.unipush.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.twb.unipush.config.UniPushProperties;
import com.twb.unipush.entity.PushMessage;

import cn.hutool.json.JSONUtil;

/**
 * @ClassName UniPushUtil
 * @Description uniPush消息推送
 * @author DSY
 * @date 2021年6月30日 下午5:27:26
 */
@Component
public class UniPushUtil {
	
	protected static Logger log = LoggerFactory.getLogger(UniPushUtil.class);

	// STEP1：获取应用基本信息
	private static UniPushProperties uniPushProperties;

	@Autowired
	public void setUniPushProperties(UniPushProperties uniPushProperties) {
		UniPushUtil.uniPushProperties = uniPushProperties;
	}

	/**
	 * @params pushMessage推送消息
	 * @params appPushList推送角色目标列表
	 */
	public static void pushMessage(PushMessage pushMessage, List<String> clientIds) {

		if (CollectionUtils.isEmpty(clientIds)) {
			log.error("APP推送角色目标列表为空, 推送失败");
			return;
		}
		
		IGtPush push = new IGtPush(uniPushProperties.getUrl(), uniPushProperties.getAppKey(), uniPushProperties.getMasterSecret());

		Style0 style = new Style0();
		// STEP2：设置推送标题、推送内容
		style.setTitle(pushMessage.getTitle());
		style.setText(pushMessage.getContent());
		style.setLogo("push_small.png");
//	        style.setLogo("push.png"); // 设置推送图标
		// STEP3：设置响铃、震动等推送效果
		style.setRing(true); // 设置响铃
		style.setVibrate(true); // 设置震动

		// STEP4：选择通知模板
		NotificationTemplate template = new NotificationTemplate();
		template.setAppId(uniPushProperties.getAppId());
		template.setAppkey(uniPushProperties.getAppKey());
		template.setStyle(style);
		// 点击消息打开应用
		template.setTransmissionType(1);
		// 传递自定义消息
		template.setTransmissionContent(JSONUtil.toJsonStr(pushMessage));

		// STEP5：定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
		// 采用toList方案，定义ListMessage消息类型
//	        List<String> appIds = new ArrayList<String>();
//	        appIds.add(appId);
		ListMessage message = new ListMessage();
		message.setData(template);
//	        message.setAppIdList(appIds);
		message.setOffline(true);
		message.setOfflineExpireTime(1000 * 600); // 时间单位为毫秒

		String contentId = push.getContentId(message);

		// 获取推送目标
		List<Target> targets = new ArrayList<Target>();

		for (String clientId : clientIds) {
			Target target = new Target();
			target.setAppId(uniPushProperties.getAppId());
			target.setClientId(clientId);
			targets.add(target);
		}

		// STEP6：执行推送，不采用toApp方案，采用toList方案
//	  IPushResult ret = push.pushMessageToApp(message);

		IPushResult ret = push.pushMessageToList(contentId, targets);
		log.info("==========[uni push][{}]", ret.getResponse().toString());
	}

}
